package edu.bw.controller.upload;

import edu.bw.utils.ResponseData;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.UUID;
import java.util.stream.Collectors;

@Log4j2
@RestController
@RequestMapping("/upload")
public class UploadController {

    private static final String UPLOAD_DIR_PATH="D:\\BaiduNetdiskDownload\\SpringMVC原理与实战\\day18_springmvc框架实战_代码\\biwujiaoyu-javaee-project\\day12-springmvc\\springmvc02\\src\\main\\webapp\\upload";

    @PostMapping("/single")
    public ResponseData singleFileUpload(@RequestParam("uploadFile") MultipartFile uploadFile){
        if (uploadFile.isEmpty()) {
            return new ResponseData(400,"上传文件为空!");
        }

        try {
            //处理文件名保证唯一
            String fileName = uploadFile.getOriginalFilename();//例如上传1.jpg
            String extension = fileName.substring((fileName.lastIndexOf(".")));
            String newFileName = UUID.randomUUID() + extension;

            //将上传得文件存到服务端
            File uploadDir = new File(UPLOAD_DIR_PATH);

            if (!uploadDir.exists()) {//如果指定得路径(文件夹)不存在,直接创建
                uploadDir.mkdirs();
            }

            //拼接路径名称
            File serviceFile = new File(uploadDir, newFileName);//最终路径:  xxx/upload/上传得文件
            uploadFile.transferTo(serviceFile);//将上传的文件写入到指定路径

            return new ResponseData(200,fileName+"上传成功!");
        } catch (IOException e) {
            log.error("文件上传失败",e);
            return new ResponseData(500,"上传失败"+e.getMessage());
        }
    }



    @PostMapping("/multiple")
    public ResponseData multipleFileUpload(@RequestParam("uploadFiles") MultipartFile[] uploadFiles){
        if (uploadFiles.length==0 || uploadFiles[0].isEmpty()) {
            return new ResponseData(400,"上传文件为空!");
        }

        File uploadDir = new File(UPLOAD_DIR_PATH);
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
        }

        ArrayList<String> uploadFileNames = new ArrayList<>();//存储已经上传的文件名称

        try {
            for (MultipartFile file : uploadFiles) {
                if (!file.isEmpty()) {
                    String fileName = file.getOriginalFilename();
                    String extension = fileName.substring(fileName.lastIndexOf("."));
                    String newFileName = UUID.randomUUID() + extension;
                    File serverFile = new File(uploadDir, newFileName);
                    file.transferTo(serverFile);
                    uploadFileNames.add(fileName);
                }
            }
            String fileNames = uploadFileNames.stream().collect(Collectors.joining(","));
            return new ResponseData(200,fileNames+"上传成功!");
        } catch (IOException e) {
            log.error("文件上传失败",e);
            return new ResponseData(500,"上传失败"+e.getMessage());
        }

    }

}
